import React, { PropTypes, Component } from 'react';
import { Link } from 'react-router'
import { getAuthShop } from './../publicAPI/publicAPI'
import style from './AssistantInput.css';
import { myAjaxJson, myAjaxGet, ROOTURL } from './../publicAPI/publicAPI';




export default React.createClass({
	getInitialState: function() {
		return {
		    topicArray: [],
		};
	},

	componentWillMount: function() {
		var _this = this;		

	},
	componentDidMount: function() {
		let _this = this;

		this.handleGetInfo();
	},
	handleGetInfo: function() {
            var _this = this;

            //列出片段记录信道信息
			myAjaxJson(ROOTURL + 'snippet/topic/search.api', 'POST', {}, function(data) {
				console.log(data);
				this.setState({
                   topicArray: data,
                });
			}.bind(_this), function(error) {
				console.log('请求失败');
			})
	},

	handleSubmit: function() {
		let _this = this;
			
		//判断信息是否填写完整
		if(!this.refs.topic.value) {
			alert('不能为空!');
			return
		}

			let param = {};
			param.id = this.refs.topic.value;
			console.log(param.id);
			param.stValid = true;
			//param.meta = {
				//topicName: '测试1',
			//}
			console.log(param);
			myAjaxJson(ROOTURL + 'snippet/topic/instance.api', 'POST', param, function(data3) {
				let _this = this;
				console.log(data3);
				//更新信息
				_this.handleGetInfo();
			}.bind(_this), function(error3) {
				alert('录入失败');
			});
	},

	handleKeyPress: function(e) {
	    if(e.which == 13) {
	        this.handleSubmit();
	    }
	},
	handleDelete: function(id) {
		let _this = this;

		myAjaxJson(ROOTURL + 'snippet/topic/instance.api?id=' + id, 'DELETE', {}, function(data) {
			console.log('删除成功');
			_this.handleGetInfo();
		}, function(error) {});
	},

	render: function() {
			return (
				<div className="rt-wrap">
					<div>
						<div>
							<p>已有字段</p>
							{
								!this.state.topicArray[0]||this.state.topicArray.map((topicObject, idx) => {
									if(topicObject.stValid) {
										return(
											<p key={topicObject.id}>
												<span>{topicObject.id}</span>
												<button onClick={this.handleDelete.bind(this, topicObject.id)}>--删除</button>
											</p>
										);
									}
								})
							}
						</div>
						<div>
			    			<div className={style["assInput_M"]}>
							    <p><label htmlFor="topic">字段：</label><input type="text" id="topic" ref="topic" /></p>
						    	<button onClick={this.handleSubmit} style={{marginLeft: '0'}} className={style["assInput_button"]}>保存</button>
								<Link to={`/DrivingSchool`}>
									<button className={style["assInput_button"]}>返回</button>
								</Link>
						    </div>
			    		</div>
	    			</div>
	    		</div>
			);
	}
});
    
